#include "GameLocation.h"

#include <SFML/Window/Keyboard.hpp>
#include <SFML/Window/Mouse.hpp>

#include "GameApplication.h"
#include "DummyObject.h"
#include "Player.h"
#include "PauseMenu.h"

using namespace sf;
using namespace mega;


GameLocation::GameLocation()
	: GameView()
	, m_objectsAmount(1)
	, m_objects(new GameObject*[m_objectsAmount])
	, m_player(new Player)
{
	m_objects[0] = new DummyObject;
}


GameLocation::~GameLocation()
{
	for (size_t i = 0; i < m_objectsAmount; ++i)
	{
		delete m_objects[i];
		m_objects[i] = nullptr;
	}

	delete [] m_objects;
	m_objects = nullptr;

	delete m_player;
	m_player = nullptr;
}


void GameLocation::render(mega::RenderTarget & _render)
{
	updateState();

	for (size_t i = 0; i < m_objectsAmount; ++i)
		m_objects[i]->render(gApp().window());

	m_player->render(gApp().window());
}


void GameLocation::updateState()
{
	if (m_moving.right)
		for (size_t i = 0; i < m_objectsAmount; ++i)
			m_objects[i]->move(Position(1., 0.));

	if (m_moving.left)
		for (size_t i = 0; i < m_objectsAmount; ++i)
			m_objects[i]->move(Position(-1., 0.));

	if (m_moving.up)
		for (size_t i = 0; i < m_objectsAmount; ++i)
			m_objects[i]->move(Position(0., -1.));

	if (m_moving.down)
		for (size_t i = 0; i < m_objectsAmount; ++i)
			m_objects[i]->move(Position(0., 1.));
}


bool GameLocation::onKeyPressed(const Keyboard::Key & _key)
{
	// return true if player handled event
	if (m_player->onKeyPressed(_key))
		return true;

	// else try to handle event by location itself
	bool isHandled = false;

	if (Keyboard::Right == _key)
	{
		m_moving.right = true;
		isHandled      = true;
	}

	if (Keyboard::Left == _key)
	{
		m_moving.left = true;
		isHandled     = true;
	}

	if (Keyboard::Up == _key)
	{
		m_moving.up = true;
		isHandled   = true;
	}

	if (Keyboard::Down == _key)
	{
		m_moving.down = true;
		isHandled      = true;
	}

	return isHandled;
}


bool GameLocation::onKeyReleased(const Keyboard::Key & _key)
{
	bool isHandled = false;

	// return true if player handled event
	if (m_player->onKeyReleased(_key))
		return true;

	// else try to handle event by location itself
	MovingDirections wasMoving = m_moving;

	if (Keyboard::Right == _key)
	{
		m_moving.right = false;
		isHandled      = true;
	}

	if (Keyboard::Left == _key)
	{
		m_moving.left = false;
		isHandled     = true;
	}

	if (Keyboard::Up == _key)
	{
		m_moving.up = false;
		isHandled   = true;
	}

	if (Keyboard::Down == _key)
	{
		m_moving.down = false;
		isHandled     = true;
	}

	if (Keyboard::P == _key)
	{
		gApp().pushView(new PauseMenu());
		isHandled = true;
	}

	return isHandled;
}


bool GameLocation::onMousePressed(const sf::Mouse::Button & _button, const Position & _pos)
{
	// return true if player handled event
	if (m_player->onMousePressed(_button, _pos))
		return true;

	// location itself doesn't provide handling for this event
	return false;
}


bool GameLocation::onMouseReleased(const Mouse::Button & _button, const Position & _pos)
{
	// return true if player handled event
	if (m_player->onMouseReleased(_button, _pos))
		return true;

	// location itself doesn't provide handling for this event
	return false;
}
